package cn.mall.bill.controller;

import cn.mall.bill.query.BillUserNameQuery;
import cn.mall.bill.service.IBillService;
import cn.mall.bill.domain.Bill;
import cn.mall.bill.query.BillQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import cn.mall.bill.vo.BillVo;
import cn.mall.order.dto.DeleteDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bill" )
public class BillController {
    @Autowired
    public IBillService billService;


    /**
     * 保存和修改公用的
     * @param bill 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Bill bill) {
        try {
            if ( bill.getId() != null)
                billService.update(bill);
            else
                billService.insert(bill);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            billService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
    */
    @PatchMapping
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            billService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            Bill bill =billService.loadById(id);
            return AjaxResult.me().setResultObj(bill);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List< Bill> list = billService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult pageList(@RequestBody BillQuery query) {
        try {
            PageList<Bill> pageList = billService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/searchByusername")
    public AjaxResult searchByusername(@RequestBody BillUserNameQuery query) {
        try {
            PageList<Bill> billPageList = billService.searchByusername(query);
            return AjaxResult.me().setResultObj(billPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    @PostMapping("/getBillBymoeny")
    public AjaxResult getBillBymoeny(@RequestBody BillVo billVo){
        try {
//            if(billVo == null){
//                return AjaxResult.me().setSuccess(false).setMessage("数据有误");
//            }
            BillVo billVo2 = billService.getBillBymoeny(billVo);
            if(billVo2 == null){
                return AjaxResult.me().setSuccess(false).setMessage("无当日记录");
            }
            billVo2.setTime(billVo.getTime());
            billVo2.setType(billVo.getType());
            return AjaxResult.me().setResultObj(billVo2);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询账单失败！" + e.getMessage());
        }
    }

    @PostMapping("/deleteBytime")
    public AjaxResult deleteBytime(@RequestBody DeleteDto dto) {
        try {
            String time = dto.getTime();
            billService.deleteBytime(time);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！" + e.getMessage());
        }
    }



}
